﻿#include "EvilMir.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/Data/Stat.h"
#include "../../../Shared/Globals.h"
#include "../../../Shared/ServerPackets.h"
#include "../DelayedAction.h"

using namespace Server::MirDatabase;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    Server::MirObjects::MapObject *EvilMir::getTarget() const
    {
        return _target;
    }

    void EvilMir::setTarget(MapObject *value)
    {
        if (_target == value)
        {
            return;
        }
        _target = value;

        if (value == nullptr && getDragonLink())
        {
            getEnvir()->DragonSystem->DeLevelTime = getEnvir()->getTime() + getEnvir()->DragonSystem->DeLevelDelay;
        }
    }

    bool EvilMir::getDragonLink() const
    {
        return _dragonlink && getEnvir()->DragonSystem != nullptr;
    }

    void EvilMir::setDragonLink(bool value)
    {
        if (_dragonlink == value)
        {
            return;
        }

        _dragonlink = value;
    }

    bool EvilMir::getCanAttack() const
    {
        return !Sleeping && MonsterObject::getCanAttack();
    }

    bool EvilMir::getCanMove() const
    {
        return false;
    }

    EvilMir::EvilMir(MonsterInfo *info) : MonsterObject(info)
    {
        setDirection(MirDirection::Up);
    }

    void EvilMir::Turn(MirDirection dir)
    {
    }

    bool EvilMir::Walk(MirDirection dir)
    {
        return false;
    }

    bool EvilMir::IsAttackTarget(MonsterObject *attacker)
    {
        return !Sleeping && MonsterObject::IsAttackTarget(attacker);
    }

    bool EvilMir::IsAttackTarget(PlayerObject *attacker)
    {
        return !Sleeping && MonsterObject::IsAttackTarget(attacker);
    }

    bool EvilMir::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), Info->ViewRange);
    }

    void EvilMir::ProcessRoam()
    {
    }

    void EvilMir::ProcessSearch()
    {
        if (!Sleeping)
        {
            MonsterObject::ProcessSearch();
        }
    }

    void EvilMir::ProcessAI()
    {
        if (!Dead && Sleeping && getEnvir()->getTime() > WakeUpTime)
        {
            Sleeping = false;
            HP = Stats[Stat::HP];
            return;
        }

        MonsterObject::ProcessAI();
    }

    void EvilMir::CompleteAttack(std::vector<std::any> &data)
    {
        if (getTarget() == nullptr)
        {
            return;
        }

        if (!getTarget()->IsAttackTarget(this) || getTarget()->getCurrentMap() != getCurrentMap() || getTarget()->Node == nullptr)
        {
            return;
        }

        std::vector<MapObject*> targets = MassAttack ? FindAllTargets(17, getCurrentLocation(), false) : FindAllTargets(2, getTarget()->getCurrentLocation(), false);
        if (targets.empty())
        {
            return;
        }

        for (int i = 0; i < targets.size(); i++)
        {
            setTarget(targets[i]);
            Attack();
        }
    }

    void EvilMir::ProcessTarget()
    {
        if (getTarget() == nullptr)
        {
            return;
        }
        if (!getCanAttack() || !InAttackRange())
        {
            return;
        }

        ShockTime = 0;
        if (getDragonLink())
        {
            if (getEnvir()->DragonSystem->Info->Level < Globals::MaxDragonLevel)
            {
                getEnvir()->DragonSystem->DeLevelTime = getEnvir()->getTime() + getEnvir()->DragonSystem->DeLevelDelay;
            }
            else
            {
                getEnvir()->DragonSystem->DeLevelTime = getEnvir()->getTime() + (6 * getEnvir()->DragonSystem->DeLevelDelay);
            }
        }

        unsigned char random = getDragonLink() ? static_cast<unsigned char>(getEnvir()->DragonSystem->MaxLevel + 3 - getEnvir()->DragonSystem->Info->Level) : static_cast<unsigned char>(8);

        if (getEnvir()->Random->Next(random) > 0) //in theory it shouldnt fire 'behind' it, but it should shoot at stuff in it's top left corner (and this code made it only hit below him not 'infront' of him)
        {
            MassAttack = false;
            setDirection(SetDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation())));
            S::ObjectRangeAttack *tempVar = new S::ObjectRangeAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            tempVar->TargetID = getTarget()->ObjectID;
            Broadcast(tempVar);
            int delay = Functions::MaxDistance(getCurrentLocation(), getTarget()->getCurrentLocation()) * 50 + 620; //50 MS per Step

            DelayedAction tempVar2(DelayedType::Damage, getEnvir()->getTime() + delay);
            ActionList.push_back(&tempVar2);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            MassAttack = true;
            S::ObjectAttack *tempVar3 = new S::ObjectAttack();
            tempVar3->ObjectID = ObjectID;
            tempVar3->Direction = getDirection();
            tempVar3->Location = getCurrentLocation();
            Broadcast(tempVar3);
            DelayedAction tempVar4(DelayedType::Damage, getEnvir()->getTime() + 500);
            ActionList.push_back(&tempVar4);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
        }
        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
    }

    void EvilMir::Attack()
    {
        int damage = GetAttackPower(Stats[Stat::MinDC], getDragonLink() ? Stats[Stat::MaxDC] + (getEnvir()->DragonSystem->Info->Level - 1 * 10) : Stats[Stat::MaxDC]);
        if (!MassAttack)
        {
            damage = static_cast<int>(damage * 0.75); //make mass attacking do slightly more dmg then targeted
        }
        if (damage == 0)
        {
            return;
        }

        if (getTarget()->Attacked(this, damage, DefenceType::MAC) <= 0)
        {
            return;
        }

        PoisonTarget(getTarget(), 5, 15, PoisonType::Green, 2000);
        PoisonTarget(getTarget(), 5, 5, PoisonType::Paralysis, 1000);
    }

    int EvilMir::Attacked(MonsterObject *attacker, int damage, DefenceType type)
    {
        return Sleeping ? 0 : MonsterObject::Attacked(attacker, damage, type);
    }

    int EvilMir::Attacked(PlayerObject *attacker, int damage, DefenceType type, bool damageWeapon)
    {
        return Sleeping ? 0 : MonsterObject::Attacked(attacker, damage, type, damageWeapon);
    }

    int EvilMir::Struck(int damage, DefenceType type)
    {
        return 0;
    }

    void EvilMir::ChangeHP(int amount)
    {
        if (getDragonLink() && amount < 0)
        {
            getEnvir()->DragonSystem->GainExp(getEnvir()->Random->Next(1, 40));
        }
        MonsterObject::ChangeHP(amount);
    }

    void EvilMir::Die()
    {
        if (Dead || Sleeping)
        {
            return;
        }

        if (!getDragonLink())
        {
            MonsterObject::Die();
        }
        else
        {
            if (Info->HasDieScript && (getEnvir()->MonsterNPC != nullptr))
            {
                getEnvir()->MonsterNPC->Call(this,StringHelper::formatSimple("[@_DIE({0})]", Info->Index));
            }
            getEnvir()->DragonSystem->GainExp(250); //why would hitting em give you so little 'points', while hitting them gives so much
            Sleeping = true;
            WakeUpTime = getEnvir()->getTime() + 5 * (60 * 1000);
        }
    }

    MirDirection EvilMir::SetDirection(MirDirection dir)
    {
        switch (dir)
        {
            case MirDirection::DownRight:
            case MirDirection::Right:
                return MirDirection::Up;
            case MirDirection::Left:
            case MirDirection::UpLeft:
                return MirDirection::Right;
            default:
                return MirDirection::UpRight;
        }
    }
}
